
jQuery(document).ready(function() {

    $('#registerForm').validate({
        rules:{
            username: {
                required: true,
                username: true,
                rangelength: [3,22]
                //remote:{
                //    url: path + "/hasPrincipal",
                //    type: "get",
                //    dataType: "json"  //接收 json 类型的数据
                //}
            },
            email: {
                required: true,
                email: true
                //remote:{
                //    url: path + "/hasPrincipal",
                //    type: "get",
                //    dataType: "json"
                //}
            },
            password: {
                required: true,
                minlength: 6
            },
            confirmPassword: {
                required: true,
                equalTo: "#password"
            }
        },
        messages: {
            username: {
                //remote: "用户名已存在！"
            },
            email: {
                //remote: "邮箱已被注册！"
            },
            confirmPassword: {
                equalTo: "两次输入的密码不一致"
            }
        },
        submitHandler: function(form){
            waitMessage();
            $.ajax({
                    type: "post",
                    url: path+"/register",
                    data: {
                        username: $('#username').val(),
                        email: $('#email').val(),
                        password: $('#password').val()
                    },
                    dataType: 'json',
                    success: function(data){
                        if(data.success){
                            swal({
                                title: "注册成功",
                                text: "我们向您的邮箱发送了一封邮件，激活账号后即可登录",
                                type: "success",
                                confirmButtonText: "确定"
                            },function(){
                                window.location = (path + "/login");
                            });
                        }
                        else errorMessage("注册失败：" + data.msg);
                    },
                    // 通常 textStatus 和 errorThrown 之中只有一个会包含信息
                    error: function(XMLHttpRequest, textStatus, errorThrown){
                        //this：调用本次AJAX请求时传递的options参数
                        errorMessage("注册失败，错误代码： " + XMLHttpRequest.status);
                    }
                }
            );
        }
    });
});

